import api.*;
import client.Mywraper;
import centrecommande.Requester;
import snippet.realTimeBar;

import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created with IntelliJ IDEA.
 * User: keita
 * Date: 03/06/13
 * Time: 00:17
 * To change this template use File | Settings | File Templates.
 */
public class myMain  implements Observer{
    double price =0, diff=0, temprice, tempdif;


    public static void main(String[] args) {
        myMain myMain = new myMain();

        myMain.Lauch();
/////////////////////////////////////////////////////////////////////


    }

    private  void Lauch() {
        final Timer timer = new Timer();
        final Mywraper mywraper =Mywraper.getInstance();
        final Requester requester=new Requester();

        final int port = 4001;  //4001 ou 7496
        final String ip = "192.168.85.54";
        final EClientSocket socket =new EClientSocket(mywraper);
        socket.eConnect(ip,port,1);
        if (socket.isConnected()) {
            System.out.println("Connexion OK");
            socket.reqCurrentTime();
        }
        final int pass=90;
        final int cdRequestId = 2 ;


        class timerMain extends TimerTask {


            int nbrRepetitions = 60;

            public void run() {
                realTimeBar bar = new realTimeBar();

                socket.reqMktData(29,requester.getListeInstrument().get(1),"",true);
                //socket.reqNewsBulletins(true);
                //socket.reqAccountUpdates(true,"DU106344");

                // socket.reqRealTimeBars(cdRequestId+1,contract,bar.getBarSize(),bar.getWhatToShow(),bar.isUseRTH());




                if (nbrRepetitions > 0) {
                    System.out.println(nbrRepetitions);
                    for (int i = 0; i<pass ; i++) {
                        try {
                            if (socket.isConnected()==false) {
                                System.out.println("Not Conected");
                                socket.eConnect(ip,port,1);
                            }
                            //socket.reqMktData(nbrRepetitions,requester.getListeInstrument().get(2),"",true);




                            //socket.reqNewsBulletins(true);
                           // socket.reqContractDetails( 1, contract );
                            //socket.reqMktData(29,contract,"165",false);
                            // Real TIme Bar

                        } catch (Throwable t) {
                            // handle error
                        }
                    }
                    nbrRepetitions--;
                } else {
                    System.out.println("Terminé!");
                    timer.cancel();
                }
            }
        }


        timer.schedule(new timerMain(), 500, 1*1000);
    }


    @Override
    public void update(Observable observable, Object o) {


    }

}
